﻿using System.Windows;
using Caliburn.Micro;
using Microsoft.Phone.Controls;
using SverigesRadio.SverigesRadioApp.Business;
using SverigesRadio.SverigesRadioApp.WindowsPhoneApplication.ViewModels.Channels;
using SverigesRadio.SverigesRadioApp.WindowsPhoneApplication.ViewModels.Programs;

namespace SverigesRadio.SverigesRadioApp.WindowsPhoneApplication
{
    public class MainPageViewModel : ViewModels.NavigatableViewModelBase
    {
        public MainPageViewModel(INavigationService navigationService, IDataContext dataContext) : 
            base(navigationService, dataContext)
        {
        }

        public void MyPivotItemLoaded(PivotItemEventArgs e)
        {
            IMainPagePivotViewModel viewModel = null;

            switch(e.Item.Header.ToString().ToLower())
            {
                case "program":
                    viewModel = ProgramCategoriesViewModel;
                    break;
                case "direkt":
                    viewModel = DirectChannelViewModel;
                    break;
            }

            if(viewModel != null)
                viewModel.Loaded();
        }

        private ProgramCategoriesViewModel programCategoriesViewModel;
        public ProgramCategoriesViewModel ProgramCategoriesViewModel
        {
            get
            {
                if (programCategoriesViewModel == null)
                {
                    programCategoriesViewModel = new ProgramCategoriesViewModel(this);
                }

                return programCategoriesViewModel;
            }

        }

        private DirectChannelViewModel directChannelViewModel;
        public DirectChannelViewModel DirectChannelViewModel
        {
            get
            {
                if (directChannelViewModel == null)
                    directChannelViewModel = new DirectChannelViewModel(this);

                return directChannelViewModel;
            }
        }


        //    private SverigesRadioServiceCaller<Channels> channelsService;
    //    private SverigesRadioServiceCaller<BroadcastInfo> broadcastService;
        
    //    public bool channelDataLoaded;
    //    public bool broadcastDataLoaded;

    //    public IDictionary<int, IPivotViewModel> pivotViewModels;


    //    public string Name { get; set; }


    //    private INavigationService navigationService;



    //    public List<String> Items
    //    {
    //        get;
    //        set;
    //    }


    //    public void LoadChannelData()
    //    {
    //        channelDataLoaded = false;

    //        channelsService.CallAsync(SverigesRadioServices.Channels);
    //    }

    //    public void LoadBroadcastData()
    //    {
    //        broadcastDataLoaded = false;

    //        broadcastService.CallAsync(SverigesRadioServices.BroadcastInfo);
    //    }

    //    void OnChannelsServiceCallReceived(object sender, SverigesRadioServiceCallerEventArgs<Channels> e)
    //    {
    //        channelDataLoaded = true;

    //        if (broadcastDataLoaded)
    //            IsBusy = false;

    //        ChannelInformationViewModels = new ObservableCollection<ChannelInformationViewModel>();
            
    //        e.ServiceResult.Channel.ForEach(p => ChannelInformationViewModels.Add(new ChannelInformationViewModel(p)));
    //        ChannelInformationViewModelsDictionary = ChannelInformationViewModels.ToDictionary(p => p.Id, p => p);

    //        LoadBroadcastData();
    //    }

    //    private void OnBroadcastServiceCallReceived(object sender, SverigesRadioServiceCallerEventArgs<BroadcastInfo> e)
    //    {
    //        broadcastDataLoaded = true;

    //        if (channelDataLoaded)
    //            IsBusy = false;

    //        foreach (var broadCastChannel in e.ServiceResult.Channels)
    //        {
    //            if (!ChannelInformationViewModelsDictionary.ContainsKey(broadCastChannel.Id))
    //                break;

    //            var channelViewModel = ChannelInformationViewModelsDictionary[broadCastChannel.Id];

    //            channelViewModel.CurrentProgramTitle = broadCastChannel.ProgramTitle;
    //            channelViewModel.CurrentProgramInfo = broadCastChannel.ProgramInfo;
    //            channelViewModel.NextProgramTitle = broadCastChannel.NextProgramTitle;
    //            channelViewModel.NextProgramStartTime = broadCastChannel.NextProgramStartTime;
    //            channelViewModel.NextProgramStopTime = broadCastChannel.NextProgramStartTime;
    //            channelViewModel.ProgramUrl = broadCastChannel.ProgramURL;
    //            channelViewModel.NextProgramUrl = broadCastChannel.NextProgramURL;

    //        }

    //        IsDataLoaded = true;

    //    }

    //    public Visibility DarkThemeVisibility
    //    {
    //        get 
    //        { 
    //            var darkVisibility = (Visibility) Application.Current.Resources["PhoneDarkThemeVisibility"];

    //            return darkVisibility;
    //        }
    //    }

    //    private DirektControlViewModel direktControlViewModel;
    //    public DirektControlViewModel DirektControlViewModel
    //    {
    //        get
    //        {
    //            if (direktControlViewModel == null)
    //            {
    //                direktControlViewModel = new DirektControlViewModel(this);
    //                pivotViewModels.Add(2, direktControlViewModel);
    //            }

    //            return direktControlViewModel;
    //        }
    //    }



    //    private ProgramsPageViewModel programsPagePageViewModel;

    //    public ProgramsPageViewModel ProgramsPagePagePageViewModel
    //    {
    //        get
    //        {
    //            if(programsPagePageViewModel == null)
    //            {
    //                programsPagePageViewModel = new ProgramsPageViewModel(this);
    //            }
    //            return programsPagePageViewModel;
    //        }
    //    }



    //    public PlayerViewModel PlayerViewModel
    //    {
    //        get
    //        {
    //            if (playerViewModel == null)
    //                playerViewModel = new PlayerViewModel(this);

    //            return playerViewModel;
    //        }
    //    }

    //    public ObservableCollection<ChannelInformationViewModel> ChannelInformationViewModels { get; set; }
    //    public Dictionary<string, ChannelInformationViewModel> ChannelInformationViewModelsDictionary { get; set; }


    //    public ICommand NavigateToPlayerCommand
    //    {
    //        get { return new Command(null, p => NavigateToPlayer()); }
    //    }

    //    private void NavigateToPlayer()
    //    {
    //       // NavigationServiceTemp.NavigateTo(ViewType.Player);
    //    }



    //    public bool IsBusyEnabled
    //    {
    //        get { return !IsBusy; }

    //    }

    //    private string _sampleProperty = "Sample Runtime Property Value";
    //    private PlayerViewModel playerViewModel;

    //    /// <summary>
    //    /// Sample ViewModel property; this property is used in the view to display its value using a Binding
    //    /// </summary>
    //    /// <returns></returns>
    //    public string SampleProperty
    //    {
    //        get
    //        {
    //            return _sampleProperty;
    //        }
    //        set
    //        {
    //            if (value != _sampleProperty)
    //            {
    //                _sampleProperty = value;

    //                //NotifyOfPropertyChange("SampleProperty");
    //            }
    //        }
    //    }

    //    public bool IsDataLoaded
    //    {
    //        get;
    //        private set;
    //    }

    //    private int selectedIndex;
    //    public int SelectedIndex
    //    {
    //        get { return selectedIndex; }
    //        set
    //        {
    //            selectedIndex = value;

    //            if (!pivotViewModels.ContainsKey(value)) return;

    //            pivotViewModels[value].IndexChanged();
    //        }
    //    }

 


    //    /// <summary>
    //    /// Creates and adds a few ItemViewModel objects into the Items collection.
    //    /// </summary>
    //    public void LoadData()
    //    {

    //        LoadChannelData();

    //    }
    }

}